package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.PermissionsMapper;
import com.cqjtu.tto.dao.mapper.PermissionsRoleMapper;
import com.cqjtu.tto.dao.mapper.RoleMapper;
import com.cqjtu.tto.dao.pojo.Permissions;
import com.cqjtu.tto.dao.pojo.PermissionsRole;
import com.cqjtu.tto.dao.pojo.Role;
import com.cqjtu.tto.service.PermissionsRoleService;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.PermissionsRoleVo;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.params.PermissionsRoleParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *@author：monkey-L
 *@description: 
 *@date: 12:27 2021/12/12
*/
@Service
public class PermissionsRoleServiceImpl implements PermissionsRoleService {

    @Autowired
    private PermissionsRoleMapper permissionsRoleMapper;

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public Results permissionsRoleAdd(PermissionsRoleParam permissionsRoleParam) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsRoleParam.getPermissionsName()).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,permissionsRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId()).last("limit 1");
        PermissionsRole permissionsRole = permissionsRoleMapper.selectOne(permissionsRoleWrapper);
        if (permissionsRole.getPermissionsId().equals(permissions.getPermissionsId())) {
            return Results.fail(1032,"该角色已存在此权限");
        }
        PermissionsRole permissionsRole1 = new PermissionsRole();
        permissionsRole1.setAuthorityRoleDeleted(0);
        permissionsRole1.setAuthorityRoleId(System.currentTimeMillis());
        permissionsRole1.setPermissionsId(permissions.getPermissionsId());
        permissionsRole1.setRoleId(role.getRoleId());
        permissionsRoleMapper.insert(permissionsRole1);
        return Results.success("添加成功！");
    }

    @Override
    @Transactional
    public Results permissionsRoleDelete(PermissionsRoleParam permissionsRoleParam) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsRoleParam.getPermissionsName()).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,permissionsRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getPermissionsId,permissions.getPermissionsId())
                .eq(PermissionsRole::getRoleId,role.getRoleId())
                .last("limit 1");
        PermissionsRole permissionsRole = permissionsRoleMapper.selectOne(permissionsRoleWrapper);
        if (permissionsRole == null) {
            return Results.fail(1034,"该角色不存在此权限，无法删除");
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        permissionsRoleLambdaQueryWrapper.eq(PermissionsRole::getAuthorityRoleId,permissionsRole.getAuthorityRoleId());
        permissionsRoleMapper.delete(permissionsRoleLambdaQueryWrapper);
        return Results.success("删除成功！");
    }

    @Override
    @Transactional
    public Results permissionsRoleUpdate(PermissionsRoleParam permissionsRoleParam) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsRoleParam.getPermissionsName()).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,permissionsRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getPermissionsId,permissions.getPermissionsId())
                .last("limit 1");
        PermissionsRole permissionsRole = permissionsRoleMapper.selectOne(permissionsRoleWrapper);
        PermissionsRole permissionsRole1 = new PermissionsRole();
        BeanUtils.copyProperties(permissionsRole,permissionsRole1);
        permissionsRole1.setPermissionsId(permissions.getPermissionsId());
        LambdaQueryWrapper<PermissionsRole> permissionsRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        permissionsRoleLambdaQueryWrapper.eq(PermissionsRole::getAuthorityRoleId,permissionsRole1.getAuthorityRoleId());
        permissionsRoleMapper.update(permissionsRole1,permissionsRoleLambdaQueryWrapper);
        return Results.success("更新成功！");
    }

    @Override
    @Transactional
    public Results permissionsRoleSelect(String roleName) {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,roleName).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId());
        PermissionsRoleVo permissionsRoleVo = new PermissionsRoleVo();
        List<PermissionsRole> permissionsRoles = permissionsRoleMapper.selectList(permissionsRoleWrapper);
        permissionsRoleVo.setRoleName(roleName);
        List<String> list = new ArrayList<>();
        int count = 0;
        for (PermissionsRole permissionsRole : permissionsRoles) {
            LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
            permissionsWrapper.eq(Permissions::getPermissionsId,permissionsRole.getPermissionsId()).last("limit 1");
            Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
            list.add(permissions.getPermissionsName());
            count++;
        }
        permissionsRoleVo.setPermissionsNumber(count);
        permissionsRoleVo.setPermissionsName(list);
        return Results.success(permissionsRoleVo);
    }
}
